package com.zoo.diannaopeijian.comm.filter;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-25.
 */
public class FilterUtil {
    private static final String idText = "id";
    private static final String textText = "text";

    public static List getFilterData(List datas, String idField, String textField) {
        List result = new ArrayList();


        for (Object obj : datas) {
            Map facter = new HashMap();
            facter.put(idText, getFieldValue(idField, obj));
            facter.put(textText, getFieldValue(textField, obj));
            result.add(facter);
        }

        return result;
    }

    private static Object getFieldValue(String fieldName, Object obj) {
        Object result = null;

        try {
            result = new PropertyDescriptor(fieldName, obj.getClass()).getReadMethod().invoke(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }

        return result;
    }
}
